import DynamicMaterialProperty from "./DynamicMaterial";
import "@/js/ImageLineMaterial";
import shortid from "shortid";
class PolyLineTest {
  constructor(viewer, points, id, color, width, img, type) {
    this.viewer = viewer;
    this.points = points;
    this.id = id;
    this.color = color;
    this.width = width;
    this.img = img;
    this.type = type;

    this.init();
  }
  init() {
    let points = Cesium.Cartesian3.fromDegreesArrayHeights(this.points);

    //计算每段比例-形成时间比例
    let lengths = [0];
    let zlength = 0;
    for (let i = 1, l = points.length; i < l; i++) {
      let l = Cesium.Cartesian3.distance(points[i - 1], points[i]);
      lengths.push(l);
      zlength += l;
    }
    this.rx = zlength / 10;
    let t = 0;
    lengths.map((len, index) => {
      if (index > 0) {
        t += len / zlength;
        lengths[index] = t;
      }
    });

    //生成样条线  提取采样点
    var spline = new Cesium.LinearSpline({
      times: lengths,
      points: points,
    });

    var positions = [];
    let n = 100;
    for (var i = 0; i <= n; i++) {
      var cartesian3 = spline.evaluate(i / n);
      positions.push(cartesian3);
    }

    //生成动态材质
    let dm = new DynamicMaterialProperty({
      name: shortid.generate(),
      url: this.img,
      repeatX: this.rx,
      repeatY: 1.0,
    });
    switch (this.type) {
      case 3:
        //生成三维管线
        const redTube1 = this.viewer.entities.add({
          name: "Red tube with rounded corners",
          id: this.id,
          polylineVolume: {
            positions: positions,
            shape: this.computeCircle(2.0),
            material: dm,
            // cornerType: Cesium.CornerType.MITERED,
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
              0.0,
              1000.0
            ),
          },
          polyline: {
            positions: positions,
            // material: dm1,
            width: this.width * 5,
            material: this.color,
            clampToGround: true,
            // cornerType: Cesium.CornerType.MITERED,
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
              1000.0,
              500000.0
            ),
          },
        });
        break;
      case 2:
        const redTube2 = this.viewer.entities.add({
          // name: "Red tube with rounded corners",
          id: this.id,
          show: false,
          zIndex: 9,
          polyline: {
            positions: positions,
            // material: dm,
            width: this.width + 4,
            material: new Cesium.ImageLineMaterialProperty({
              image: require("@/assets/0000.png"),
            }),
            //
            // distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
            //   1000.0,
            //   500000.0
            // ),
            clampToGround: true,
            // cornerType: Cesium.CornerType.MITERED,
            //   distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
            //     500,
            //     500000
            //   ),
          },
        });
        break;
      default:
        break;
    }
    // console.log(positions, "===");
  }
  computeCircle(radius) {
    const positions = [];
    for (let i = 0; i < 360; i++) {
      const radians = Cesium.Math.toRadians(i);
      positions.push(
        new Cesium.Cartesian2(
          radius * Math.cos(radians),
          radius * Math.sin(radians)
        )
      );
      i += 72;
    }
    return positions;
  }
}
export default PolyLineTest;
